//
//  MainViewController.swift
//  LXQWB
//
//  Created by 江海天 on 2017/7/1.
//  Copyright © 2017年 江海天. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
		//设置渲染模式
		//tabBar.tintColor = UIColor.orange
		//addAllChildViewController()
		//let vc = HomeTableViewController()
		/*let vc = HomeTableViewController()
		vc.tabBarItem.title = "首页"
		tabBar.tintColor = UIColor.orange
		addChildViewController(vc)
          */
        // Do any additional setup after loading the view.
    }
	override func viewWillAppear(_ animated: Bool) {
		super.viewWillAppear(true);
		
		plusButton.center = CGPoint(x: tabBar.frame.size.width*0.5, y: tabBar.frame.size.height*0.5)
		tabBar.addSubview(plusButton)
	}

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //添加一个子控制方法
	/*
	private func addChildViewController(childControllerName  childController: String?,title: String?,imageName: String?) {
		/**
			此方法会由内而外的调用
			tabar title
			nav title
		*/
		//1。根据字符串创建类
		guard let nameSpace = Bundle.main.infoDictionary?["CFBundleExecutable"] as? String else {
			return
		}
		//var cls:UITableViewController.Type? = nil
		guard let childName = childController else{
			return
		}
		
		guard let cls = NSClassFromString(nameSpace + "." + childName) as? UITableViewController.Type else{
			return
		}
		let Childvc = cls.init()
		if let title = title {
			Childvc.title = title
		}
		if let imageName = imageName {
			Childvc.tabBarItem.image = UIImage(named: imageName)
			Childvc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
		}
		let nav = UINavigationController(rootViewController: Childvc)
		addChildViewController(nav)

	}
	//添加所有子控制器
	private func addAllChildViewController() {
		//1.根据json文件
		//1.1获取json路径
		guard let path = Bundle.main.path(forResource: "Main", ofType:"json") else{
			
		return
		}
		
		let url = URL(fileURLWithPath: path)
		NLog(message: url)
		do {
			let jsonData =  try Data(contentsOf: url)
			do {
				let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers) as! [[String:AnyObject]]
				for dic in jsonObject {
					let vcName = dic["vcName"] as? String
					let title = dic["title"] as? String
					let imageName = dic["imageName"] as? String
					addChildViewController(childControllerName: vcName, title: title, imageName: imageName)
				}
				
			} catch {
				addChildViewController( childControllerName: "HomeTableViewController", title: "首页", imageName: "tabbar_home")
				addChildViewController( childControllerName: "DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
				addChildViewController( childControllerName: "NullTableViewController", title: "", imageName: "")
				addChildViewController(childControllerName: "MessageTableViewController", title: "信息", imageName: "tabbar_message_center")
				addChildViewController(childControllerName: "ProfileTableViewController", title: "我", imageName: "tabbar_profile")
				print("no json")
			}

		} catch{
			print("no JSON")
		}
				/*
		
		*/
	}
	*/
	//MARK: plus button
	private lazy var plusButton:UIButton = {
		let button = UIButton(imageName: "tabbar_compose_icon_add", imageBackgroundName:"tabbar_compose_button" )
			button.addTarget(self, action:#selector(composeMessage(sender:)) , for: UIControlEvents.touchDown)
		return button
	}()
	
	func composeMessage(sender: UIButton) {
		NLog(message: "xxxxx")
	}
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
